//
//  WeiBoSendViewController.m
//  cityStar
//
//  Created by wangjia on 15/3/28.
//  Copyright (c) 2015年 tiny. All rights reserved.
//

#import "WeiBoSendViewController.h"
#import "UIImage+fixOrientation.h"
#import "UpLoadImageManager.h"
#import "HttpRequestManager.h"
#import "KInterFace.h"
#import "SVProgressHUD.h"

@interface WeiBoSendViewController ()<UIActionSheetDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>{
    NSMutableDictionary *dictPhotoIDs;
}

@end

@implementation WeiBoSendViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.sectionHeaderHeight = 0;
    self.tableView.sectionFooterHeight = 0;
    
    self.navigationItem.title = @"发微博";
    
    dictPhotoIDs = [[NSMutableDictionary alloc]init];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(cancelAction)];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"发送" style:UIBarButtonItemStylePlain target:self action:@selector(sendAction)];
    
    [self.txContent becomeFirstResponder];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)cancelAction{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)sendAction{
    NSDictionary *parameters = @{@"content":self.txContent.text};
    [dictPhotoIDs addEntriesFromDictionary:parameters];
    
    [HttpRequestManager postMethod:KinterFace_blogAdd parameters:dictPhotoIDs completedMainBlock:^(BOOL success, id response, NSString *errMsg) {
        if (success) {
            [SVProgressHUD showSuccessWithStatus:@"发布成功"];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"weiboRefresh" object:nil];
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            [SVProgressHUD showErrorWithStatus:errMsg];
        }
    }];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}

- (IBAction)btnSelectAction:(id)sender {
    [self selectImage];
}

/**
  *  选择头像来源
  */
- (void)selectImage{
    UIActionSheet *sheet;
    // 判断是否支持相机
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
    {
        sheet  = [[UIActionSheet alloc] initWithTitle:@"选择图像"
                                             delegate:self
                                    cancelButtonTitle:nil
                               destructiveButtonTitle:@"拍照"
                                    otherButtonTitles:@"从相册选择",@"取消", nil];
    }
    else
    {
        sheet = [[UIActionSheet alloc] initWithTitle:@"选择图像"
                                            delegate:self
                                   cancelButtonTitle:nil
                              destructiveButtonTitle:@"从相册选择"
                                   otherButtonTitles:@"取消", nil];
    }
    [sheet showInView:self.view];
}

#pragma mark - action sheet delegte
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSUInteger sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    // 判断是否支持相机
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
    {
        switch (buttonIndex)
        {
            case 2: //取消
                
                return;
            case 0: //相机
                sourceType = UIImagePickerControllerSourceTypeCamera;
                break;
            case 1: //相册
                
                sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                break;
        }
    }
    else//不支持相机
    {
        if (buttonIndex == 1)
        {
            return;
        }
        if (buttonIndex == 0)
        {
            sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
        }
    }
    // 跳转到相机或相册页面
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
    imagePickerController.delegate = self;
//    imagePickerController.allowsEditing = YES;
    imagePickerController.sourceType = sourceType;
    
    [self presentViewController:imagePickerController animated:YES completion:nil];
    
}

#pragma mark - image picker delegte

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [picker dismissViewControllerAnimated:YES completion:^{}];
    UIImage *image=[info objectForKey:UIImagePickerControllerOriginalImage];
    image = image.fixOrientation;
    [self.ivContent setImage:image];
    
    [UpLoadImageManager uploadImage:image withCompltedBlock:^(BOOL success, NSString *urlString) {
        if (success) {
            [dictPhotoIDs setObject:urlString forKey:@"image[0]"];
        }
    }];
}
@end
